package com.linln.modules.system.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linln.common.utils.UUIDGenerator;
import com.linln.modules.system.dao.BeautyCompanyMapper;
import com.linln.modules.system.dao.BeautyProductMapper;
import com.linln.modules.system.dao.CustomMapper;
import com.linln.modules.system.entity.BeautyCompany;
import com.linln.modules.system.entity.BeautyCompanyExample;
import com.linln.modules.system.entity.BeautyProduct;
import com.linln.modules.system.entity.BeautyProductExample;
import com.linln.modules.system.vo.admins.ProductCompanyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Service
public class CompanyService {

    @Autowired
    private BeautyCompanyMapper companyMapper;

    @Autowired
    private BeautyProductMapper productMapper;

    public Page<BeautyCompany> getIndexCompany(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<BeautyCompany> pager = companyMapper.getAllCompanyPager(name);
        return pager;
    }

    public List<BeautyCompany> getCompanyList(){
        BeautyCompanyExample example = new BeautyCompanyExample();
        example.createCriteria().andDeletedEqualTo(0).andStatesEqualTo(0);
        List<BeautyCompany> companies = companyMapper.selectByExample(example);
        return companies;
    }

    public List<BeautyProduct> getProductList(String companyId){
        BeautyProductExample example = new BeautyProductExample();
        example.createCriteria().andDeletedEqualTo(0).andStatesEqualTo(0).andCompanyIdEqualTo(companyId);
        List<BeautyProduct> products = productMapper.selectByExample(example);
        return products;
    }


    @Autowired
    private CustomMapper customMapper;


    public List<ProductCompanyVo> getAllProdAndCompany(){
        List<ProductCompanyVo> company = customMapper.getAllProductCompany();
        return company;
    }

    public BeautyCompany getBeautyCompanyById(String id){
        BeautyCompany company = companyMapper.selectByPrimaryKey(id);
        return company;
    }

    public int saveCompany(BeautyCompany beautyCompany){
        Date date = new Date();
        beautyCompany.setId(UUIDGenerator.generate());
        beautyCompany.setCreateTime(date);
        beautyCompany.setUpdateTime(date);
        beautyCompany.setDeleted(0);
        beautyCompany.setStates(0);

        int insert = companyMapper.insert(beautyCompany);
        return insert;
    }

    public int updateCompany(BeautyCompany beautyCompany){
        Date date = new Date();
        beautyCompany.setUpdateTime(date);
        beautyCompany.setDeleted(0);
        beautyCompany.setStates(0);
        int insert = companyMapper.updateByPrimaryKeySelective(beautyCompany);
        return insert;
    }

    public int deleteCompany(String id){
        BeautyCompany company = new BeautyCompany();
        company.setId(id);
        company.setDeleted(1);
        company.setUpdateTime(new Date());
        int insert = companyMapper.updateByPrimaryKeySelective(company);
        return insert;
    }
}
